﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;


/// <summary>
/// Summary description for GoogleMaps
/// </summary>
public static class GoogleMaps
{

    public static void createMap(System.Web.UI.Page page, double x, double y, String title)
    {

        String script;
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.Append("initializeGoogleMaps(");
        stringBuilder.Append(x.ToString());
        stringBuilder.Append(",");
        stringBuilder.Append(y.ToString());
        stringBuilder.Append(",");
        stringBuilder.Append("'" + title + "'");
        stringBuilder.Append(")");
        script = stringBuilder.ToString();

        page.ClientScript.RegisterStartupScript(page.GetType(), "initializeGoogleMaps", script, true);

    }

    public static Coord getLatLng(String address)
    {
        String url1 = String.Format(
            "http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=true"
            , HttpContext.Current.Server.UrlEncode(address));

        XmlDocument doc = new XmlDocument();
        doc.Load(url1);
        XmlNodeList nodes = doc.SelectNodes("/GeocodeResponse/result/geometry/location");
        XmlNode node = nodes[0];

        String lat = node.SelectSingleNode("lat").InnerText;
        String lng = node.SelectSingleNode("lng").InnerText;

        Coord coord = new Coord();
        coord.lat = Convert.ToDouble(lat);
        coord.lng = Convert.ToDouble(lng);

        return coord;

    }
}